#ifndef _DATRIE_H_
#define _DATRIE_H_

/* TODO
 * dat can be compacked by move tag and freq to _da[base] + 0
 */

#define DICT_SIZE       (1<<18)		//dict alloced for words
#define DICT_ITEM_LEN   (1<<6)		//max word len
#define DAT_BLOCK_SIZE  (1<<18)		//per alloc size
#define DAT_DEF_ROOT    (1)			//index of dat root
#define DAT_RES_MAX     (1<<5)		//max result num


#define FREQ(freq)			( (freq) & 0x00FFFFFF )
#define DIV1(freq)			( ((unsigned char)( (freq) >> 24)) >> 4 )
#define DIV2(freq)			( ((unsigned char)( (freq) >> 24)) & 0x0F )
#define DIV1AT(freq, pos)	( (freq) | (((unsigned int)((pos) << 4)) << 24) ) 		//note that pos is not count by WORD but BYTE!
#define DIV2AT(freq, pos)	( (freq) | (((unsigned int)((pos) & 0x0F)) << 24) ) 		//note that pos is not count by WORD but BYTE!

typedef struct dict_item{
	char word[DICT_ITEM_LEN];
	unsigned short len;		//to decide if the word is over
	unsigned int tag;
	unsigned int freq;		//hightest 8bit is div map so 256byte dividing is supported !must be check at setup.py
} dict_item;

typedef struct dat_node{
	int base;
	int check;
	unsigned int tag;
	unsigned int freq;
} dat_node;

typedef struct dat_res_item{
	int i;
	unsigned short len;
	unsigned int tag;
	unsigned int freq;
} dat_res_item;

typedef struct dat_res{
	int num;
	dat_res_item item[DAT_RES_MAX];
} dat_res;

typedef struct dat{
	unsigned int _volume;	//volume of double array
	unsigned int _dt_size;	//size of dict table
	int _root;		//root
	int _base;		//next base position
	unsigned int _size;		//the total of all nodes
	dat_node * _da;	//double array
	dict_item * _dt; //dict table
} dat;

dat * dat_new();
int dat_free(dat * p);
int dat_fetch(const dat * p, const char * str, int depth, dat_res * res);
int dat_load(dat * p, const char * dat_path);
int dat_dump(dat * p, const char * dict_path, const char * dat_path);

#endif
